package com.dy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.pojo.entity.DyTypeEntity;
import com.dy.service.DyTypeService;
import com.dy.utils.ApiResousUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/9 16:15
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/type")
public class DyTypeController {
    @Autowired
    private DyTypeService dyTypeService;

    /**
     * 全查询
     * @return
     */
    @PostMapping("/query")
    public Page<DyTypeEntity> queryType(@RequestParam(value = "page",defaultValue ="1")Integer page,
                                        @RequestParam(value = "pageSize",defaultValue ="5")Integer pageSize){
        Page<DyTypeEntity> selectPage = dyTypeService.selectPage(page, pageSize);
        return selectPage;
    }

    /**
     * 添加分类
     * @param name
     * @return ApiResousUtil
     */
    @PostMapping("/create")
    public ApiResousUtil<String> createType(@RequestParam("name")String name){
        if (dyTypeService.create(name) == false) {
            return ApiResousUtil.error("重复分类");
        }
        if (name == null || name == "") {
            return ApiResousUtil.error("name为空");
        }
        dyTypeService.create(name);
        return ApiResousUtil.ok("添加成功");
    }

    /**
     * 删除分类
     * @param code
     * @return ApiResousUtil
     */
    @GetMapping("/deleteType")
    public ApiResousUtil<String> deleteType(@RequestParam("code")String code){
        if (code == null) {
            return ApiResousUtil.error("code为空");
        }
        dyTypeService.deleteType(code);
        return ApiResousUtil.ok("删除成功");
    }
    /**
     *
     * 根据code修改
     * @param code
     * @param name
     * @return
     **/
    @PostMapping("/updateType")
    public ApiResousUtil<String> updateType(@RequestParam String code, @RequestParam String name){
        if (code == null && code==null) {
            return ApiResousUtil.error("参数为空");
        }
        dyTypeService.updateType(code,name);
        return ApiResousUtil.ok();
    }
    /**
     * 根据code查询
     * @param code
     * @return
     **/
    @RequestMapping("/queryTypeByCode")
    public ApiResousUtil<DyTypeEntity> queryTypeByCode(@RequestParam String code){
        DyTypeEntity dyTypeEntity = dyTypeService.queryTypeByCode(code);
        return ApiResousUtil.ok(dyTypeEntity);
    }
    /**
     * 查询全部分类
     * @return
     **/
    @GetMapping("/selectTypes")
    public ApiResousUtil selectTypes(){
        List<DyTypeEntity> listTypes = dyTypeService.list();
        return ApiResousUtil.ok(listTypes);
    }
}
